;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "mhuechange.lisp")
(require "mbmptree.lisp")
(require "mavltree.lisp")
(require "mhelpers.lisp")
(require "mdatagenerators.lisp")

(module tHueChange
  (import iHueChange)
  (import iHelpers)
  (import iBmptree)
  (import iAvltree)
  (import iDataGenerators)
  (include-book "testing" :dir :teachpacks)
  (include-book "doublecheck" :dir :teachpacks) 
  (include-book "io-utilities" :dir :teachpacks) 
  (include-book "list-utilities" :dir :teachpacks)
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; DATA GENERATORS
  
  ; Generates a random pixel (random RGB values).
  (defrandom random-pixel ()
    (let* ((r (random-between 0 255))
           (g (random-between 0 255))
           (b (random-between 0 255)))
      (list r g b)))
 
  ; Random bitmap file, uses a static header with a 
  ; size of heightxwidth.
  (defrandom random-bitmap (height width)
    (let* ((header (list 66 77 54 12 0 0 0 0 0 0 54 0 0 
                         0 40 0 0 0 width 0 0 0 height 0 0 0 1 
                         0 24 0 0 0 0 0 0 12 0 0 0 0 0 
                         0 0 0 0 0 0 0 0 0 0 0 0 0))
           (pixels (random-list-of (random-pixel) :size (* width height)))
           (tr (insert-header (empty-bmp-tree) header)))
      (fill-bitmap tr 0 0 height width pixels)))
  
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; SANITY CHECKS
  (check-expect (pixel-to-hue 0 0 0 0 0) (list 0 0 0 0))
  (check-expect (pixel-to-hue 0 0 0 0 361) "Invalid RGB value.")
  (check-expect (pixel-to-hue 0 255 255 255 0) (list 0 255 255 255))
  (check-expect (pixel-to-hue 0 255 0 0 0) (list 0 255 0 0))
  (check-expect (pixel-to-hue 0 0 255 0 120) (list 0 0 255 0))
  (check-expect (pixel-to-hue 0 0 0 255 240) (list 0 0 0 255))
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; PROPERTIES
  ; pixel-to-hue returns a list of four numbers:
  ; the column number followed by three numbers between [0, 255]
  (defproperty pixel-to-hue-returns-list-of-4-prp
    (r   :value (random-natural)
         :where (<= r 255)
     g   :value (random-natural)
         :where (<= g 255)
     b   :value (random-natural)
         :where (<= b 255)
     hue :value (random-natural)
         :where (<= hue 360)
     col :value (random-natural))
    (let* ((newRGB (pixel-to-hue col r g b hue))
           (newR   (cadr newRGB))
           (newG   (caddr newRGB))
           (newB   (cadddr newRGB)))
      (and (= (len newRGB) 4)
           (and (natp newR) (<= newR 255))
           (and (natp newG) (<= newG 255))
           (and (natp newB) (<= newB 255)))))
  
  ; pixel-to-hue does not affect saturation and value in the
  ; HSV color model. The values of s and v remain the same
  ; before and after pixel-to-hue is called.
  (defproperty pixel-to-hue-s-and-v-unaffected-prp
    (r   :value (random-natural)
         :where (<= r 255)
     g   :value (random-natural)
         :where (<= g 255)
     b   :value (random-natural)
         :where (<= b 255)
     hue :value (random-natural)
         :where (<= hue 360)
     col :value (random-natural))
    (let* ((hsv    (RGB-to-HSV r g b))
           (s      (cadr hsv))
           (v      (caddr hsv))
           (newRGB (pixel-to-hue col r g b hue))
           (newR   (cadr newRGB))
           (newG   (caddr newRGB))
           (newB   (cadddr newRGB))
           (newHSV (RGB-to-HSV newR newG newB))
           (newS   (cadr newHSV))
           (newV   (caddr newHSV)))
      (and (equal s newS) (equal v newV))))
  
  ; make-hue-columns returns a list
  (defproperty make-hue-columns-returns-list-prp
    (ht  :value (random-between 1 25)
     wd  :value (random-between 1 25)
     tr  :value (random-bitmap ht wd)
     ndx :value (random-between 0 (- ht 1))
     row :value (get-row tr ndx)
     hue :value (random-natural)
         :where (<= hue 360))
    (listp (make-hue-columns row nil hue)))
  
  ; make-hue-columns returns a list containing the same number of
  ; columns that the row has originally
  (defproperty make-hue-columns-returns-correct-number-of-columns-prp
    (ht  :value (random-between 1 25)
     wd  :value (random-between 1 25)
     tr  :value (random-bitmap ht wd)
     ndx :value (random-between 0 (- ht 1))
     row :value (get-row tr ndx)
     hue :value (random-natural)
         :where (<= hue 360))
    (= (length (make-hue-columns row nil hue)) wd))
  
  ; The hue-change function always returns a bitmap tree.
  (defproperty hue-change-returns-tree-prp
    (ht  :value (random-between 1 25)
     wd  :value (random-between 1 25)
     tr  :value (random-bitmap ht wd)
     hue :value (random-natural)
         :where (<= hue 360))
    (bmp-tree? (hue-change tr hue))))
  
(link rtHueChange (mHelpers mAvlTree mBmpTree mDataGenerators mHueChange tHueChange))
(invoke rtHueChange)